package com.zzuli.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzuli.practice.entity.History;
import com.zzuli.practice.entity.Repairman;
import com.zzuli.practice.mapper.HistoryMapper;
import com.zzuli.practice.service.IHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HistoryService implements IHistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    // 添加登录历史
    public int addHistory(History history){
        return historyMapper.insert(history);
    }

    // 分页查询
    public IPage<History> historyIPage(Integer pageIndex, String username){
        if (pageIndex == null || pageIndex < 1){
            pageIndex = 1;
        }
        // 首先需要定义一个拦截器才能使用
        Page<History> page = new Page<>(pageIndex, 5);
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        IPage<History> iPage = historyMapper.selectPage(page, wrapper);
        // 判断当前页是否大于总页数
        if (iPage.getCurrent() > iPage.getPages()){
            page.setCurrent(iPage.getPages());
        }
        return historyMapper.selectPage(page, wrapper);
    }

}
